"""
写： 把内存中的对象写出到文件中
读： 把文件中的内容作为代码来执行
"""
from day10.Student import Student

# s1 = Student("宋江",32,"男","山东郓城县")
stus = [
    Student("宋江",32,"男","山东郓城县"),
    Student("卢俊义",31,"男","河北大名府"),
    Student("公孙胜",30,"男","天津"),
    Student("关胜",32,"男","山西运城"),
    Student("吴用",28,"男","山东郓城县"),
]
# 将对象写到文件中
# with open("stu.txt","w",encoding="UTF-8") as file:
#     file.write(stus.__repr__())


# # 将文件中的内容读取出来继续作为对象使用
with open("stu.txt","r",encoding="UTF-8") as file:
    # Student("宋江",32,"男","山东郓城县")
    # eval()  把括号中的内容转换成代码执行
    # s2 = file.read() 读取出来的是字符串，不是类
    s2 = eval(file.read())  # 宋江,32,男,山东郓城县
    print(type(s2))
    print(s2)
    for item in s2:
        print(item.name)
    # print(s2.name)

